﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;

namespace ShareMarking.Api
{
    public partial class ApiTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (请输入参数.Text != "")
            {
                var signature = HMACSHA1Encrypt(TrimString(请输入参数.Text), "accessuser_secret_ecrm");
                string signstr = "";
                if (signature.IndexOf("+") > 0)
                {
                    signstr = StringReplace(signature, "+", " ");//+号替换为空
                }
                else {
                    signstr = signature;
                }
                Label1.Text = signstr;
            }
            else
            {
                //var signature = HMACSHA1Encrypt("access_id=accessuser_sfa&method=gethospqrlist&timestamp=1494309488882", "accessuser_secret_sfa");
                Label1.Text = "请输入参数";
            }          
        }

        public static string HMACSHA1Encrypt(string EncryptText, string EncryptKey)
        {
            HMACSHA1 hmacsha1 = new HMACSHA1();
            hmacsha1.Key = Encoding.UTF8.GetBytes(EncryptKey);
            byte[] dataBuffer = Encoding.UTF8.GetBytes(EncryptText);
            byte[] hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
        }
        public static string HmacSha1Sign(string text, string key)
        {
            var hmacsha1 = new HMACSHA1(Encoding.UTF8.GetBytes(key));
            var dataBuffer = Encoding.UTF8.GetBytes(text);
            var hashBytes = hmacsha1.ComputeHash(dataBuffer);
            return Convert.ToBase64String(hashBytes);
            //Encoding encode = Encoding.GetEncoding("utf-8");
            //byte[] byteData = encode.GetBytes(text);
            //byte[] byteKey = encode.GetBytes(key);
            //HMACSHA1 hmac = new HMACSHA1(byteKey);
            //CryptoStream cs = new CryptoStream(Stream.Null, hmac, CryptoStreamMode.Write);
            //cs.Write(byteData, 0, byteData.Length);
            //cs.Close();
            //return Convert.ToBase64String(hmac.Hash);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str">文本</param>
        /// <param name="toRep">替换文本</param>
        /// <param name="strRep">替换文本</param>
        /// <returns></returns>
        public static string StringReplace(string str, string toRep, string strRep)
        {
            StringBuilder sb = new StringBuilder();
            int np = 0, n_ptmp = 0;
            for (; ; )
            {
                string str_tmp = str.Substring(np);
                n_ptmp = str_tmp.IndexOf(toRep);

                if (n_ptmp == -1)
                {
                    sb.Append(str_tmp);
                    break;
                }
                else
                {
                    sb.Append(str_tmp.Substring(0, n_ptmp)).Append(strRep);
                    np += n_ptmp + toRep.Length;
                }
            }
            return sb.ToString();
        }    
        /// <summary>
        /// 去空格
        /// </summary>
        /// <param name="message"></param>
        /// <returns></returns>
        public static string TrimString(string message)
        {
            if (message == "") return "";
            return Regex.Replace(message, @"\s", "");
        }
    }
}